<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计 object 中有多少个 key</title>
</head>
<body>
    <script>
        var obj = {
            name1: 'key',
            name2: 24,
            name3: 'key',
            name4: 18,
            name5: 'key',
            name6: 39,
            name7: 'key',
            name8: 'key'
        };
        var arr = [];// 创建一个空数组
        for(var sak in obj){// 遍历对象，把obj对象转换成数组
            arr.push(obj[sak]);
        }
        console.log(arr);// arr空数组，变成了arr = ['key', 24, 'key', 18, 'key', 39, 'key']

        arr = String(arr);// 将arr数组转换成字符串类型

        var scr = arr.match(/key/g);// 使用正则表达式匹配所有key，5个都key保留下来，变成了arr = "'key','key','key','key','key'"
        
        console.log('object中有' + scr.length + '个key');// 在输入它的长度就可以了
    </script>
</body>
</html>